package com.jhgsys.internal.incentive.service.impl;

import com.jhgsys.internal.incentive.entity.IncentiveRewardGoods;
import com.jhgsys.internal.incentive.entity.IncentiveRewardUser;
import com.jhgsys.internal.incentive.mapper.IncentiveRewardGoodsMapper;
import com.jhgsys.internal.incentive.service.IIncentiveRewardGoodsService;
import com.jhgsys.internal.common.entity.QueryRequest;
import com.jhgsys.internal.common.utils.FebsUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Propagation;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.toolkit.StringPool;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 安全奖励物品 Service实现
 *
 * @author wanghjw
 * @date 2024-09-03 14:08:29
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class IncentiveRewardGoodsServiceImpl extends ServiceImpl<IncentiveRewardGoodsMapper, IncentiveRewardGoods> implements IIncentiveRewardGoodsService {

    /**
     * 查询（分页）
     * @author wanghjw
     * @date 2024-09-03 14:08:29
     * @param request QueryRequest
     * @param incentiveRewardGoods incentiveRewardGoods
     * @return IPage<IncentiveRewardGoods>
    */
    @Override
    public IPage<IncentiveRewardGoods> findIncentiveRewardGoodsPage(QueryRequest request, IncentiveRewardGoods incentiveRewardGoods) {
        LambdaQueryWrapper<IncentiveRewardGoods> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(IncentiveRewardGoods::getDeletedFlg, 0);
        // TODO 设置查询条件

        Page<IncentiveRewardGoods> page = new Page<>(request.getPageNum(), request.getPageSize());
        FebsUtil.pageWrapper(page, request);
        return this.page(page, queryWrapper);
    }

    /**
     * 查询（所有）
     * @author wanghjw
     * @date 2024-09-03 14:08:29
     * @param incentiveRewardGoods incentiveRewardGoods
     * @return List<IncentiveRewardGoods>
     */
    @Override
    public List<IncentiveRewardGoods> findIncentiveRewardGoodsList(IncentiveRewardGoods incentiveRewardGoods) {
	    LambdaQueryWrapper<IncentiveRewardGoods> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(IncentiveRewardGoods::getDeletedFlg, 0);
        if(StringUtils.isNotBlank(incentiveRewardGoods.getRewardApplyId())){
            if(incentiveRewardGoods.getRewardApplyId().indexOf(StringPool.COMMA)>-1){
                List<String> applyIdList = Arrays.asList(incentiveRewardGoods.getRewardApplyId().split(StringPool.COMMA));
                queryWrapper.in(IncentiveRewardGoods::getRewardApplyId,applyIdList);
            }else {
                queryWrapper.eq(IncentiveRewardGoods::getRewardApplyId,incentiveRewardGoods.getRewardApplyId());
            }
        }
		return this.baseMapper.selectList(queryWrapper);
    }

    /**
    * 根据ID查询
    * @author wanghjw
    * @date 2024-09-03 14:08:29
    * @param id
    * @return IncentiveRewardGoods
    */
    @Override
    public IncentiveRewardGoods findIncentiveRewardGoodsById(String id){
        return this.baseMapper.selectById(id);
    }

    /**
     * 新增
     * @author wanghjw
     * @date 2024-09-03 14:08:29
     * @param incentiveRewardGoods incentiveRewardGoods
     */
    @Override
    @Transactional
    public void createIncentiveRewardGoods(IncentiveRewardGoods incentiveRewardGoods) {
        incentiveRewardGoods.setCreatedTime(new Date());
        this.save(incentiveRewardGoods);
    }

    /**
     * 修改
     * @author wanghjw
     * @date 2024-09-03 14:08:29
     * @param incentiveRewardGoods incentiveRewardGoods
     */
    @Override
    @Transactional
    public void updateIncentiveRewardGoods(IncentiveRewardGoods incentiveRewardGoods) {
        incentiveRewardGoods.setUpdatedTime(new Date());
        this.saveOrUpdate(incentiveRewardGoods);
    }

    /**
     * 逻辑删除
     * @author wanghjw
     * @date 2024-09-03 14:08:29
     * @param incentiveRewardGoods incentiveRewardGoods
     * @param incentiveRewardGoodsIds incentiveRewardGoodsIds
     */
    @Override
    @Transactional
    public void deleteIncentiveRewardGoods(IncentiveRewardGoods incentiveRewardGoods,String incentiveRewardGoodsIds) {
        List<String> list = Arrays.asList(incentiveRewardGoodsIds.split(StringPool.COMMA));
        this.baseMapper.update(incentiveRewardGoods,new LambdaQueryWrapper<IncentiveRewardGoods>().in(IncentiveRewardGoods::getRewardGoodsId,list));
	}

    /**
    * 物理删除
    * @author wanghjw
    * @date 2024-09-03 14:08:29
    * @param incentiveRewardGoodsIds incentiveRewardGoodsIds
    */
    @Override
    @Transactional
    public void physicallyDeleteIncentiveRewardGoods(String incentiveRewardGoodsIds) {
        List<String> list = Arrays.asList(incentiveRewardGoodsIds.split(StringPool.COMMA));
        LambdaQueryWrapper<IncentiveRewardGoods> wapper = new LambdaQueryWrapper<>();
        wapper.in(IncentiveRewardGoods::getRewardGoodsId,list);
        this.remove(wapper);
    }
}
